var config = require('../../utils/config');
var util = require('../../utils/util');
var app = getApp();
Page({
  data: {
    pageNo:1,
    pageSize:10,
    bookList:[],
    isLogin:false
  },
  onShow:function() {
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      that.queryAllBorrowBooks(userInfo, true);
    }else {
      //调用应用实例的方法获取全局数据
      app.getUserInfo(function(userInfo) {
        setTimeout(()=>{
          userInfo = wx.getStorageSync('user');
          if (userInfo.userId) {
            that.queryAllBorrowBooks(userInfo, true);
          }else {
            that.queryAllBorrowBooks(userInfo, false);
          }
        }, 2000);
      })
    }
  },
  queryAllBorrowBooks:function(userInfo, isLogin) {
    var that = this;
    var options = {
      url: config.clubApi.list,
      data: {
        title: '',
        grade: '',
        subject: '',
        dispart: '',
        userId: userInfo.userId,
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        var books = res.data.data.pageData;
        that.setData({
          bookList: books,
          isLogin:isLogin
        })
      }
    });
  },
  submitBook(e) {
    var that = this;
    let userInfo = wx.getStorageSync('user');
    var options = {
      url: config.clubApi.put,
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {        
        id: e.currentTarget.dataset.id,
        userId: userInfo.userId
      }
    };
    util.request(options, (res, err) => {
      if (res.statusCode===200) {
        util.showSuccess('加入成功!', config.showSuccessTime, () => {
          wx.navigateBack();
          that.onShow();
        })
      }
    });
  },
  goToContentPage:function(e) {
    var ary = e.currentTarget.id.split('^');
    wx.navigateTo({
      url: '../bookContent/bookContent?id='+ary[0]+'&userbookId='+ary[1]+'&jxkey='+ary[2]
    });
  },
  onReachBottom(){
    setTimeout(()=>{
      this.getMoreData();
    },500)
  },
  getMoreData() {
    var that = this;
    that.data.pageNo++;
    let userInfo = wx.getStorageSync('user');
    var options = {
      url: config.clubApi.list,
      data: {
        title: '',
        grade: '',
        subject: '',
        dispart: '',
        userId: userInfo.userId,
        pageNo: that.data.pageNo,
        pageSize: that.data.pageSize
      }
    };
    util.request(options, function(res) {
      if (typeof (res.data.data) !== 'undefined'&&res.data.data!=null) {
        var books = res.data.data.pageData;
        that.setData({
          bookList: books
        })
      }
    });
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
})